Ext.define('JRainbow.common.BasePanel', {
	extend : 'Ext.panel.Panel',
	layout : 'border',
	border : false,
	formColumnCount : 2,
	winWidth : 400,
	winHeigth : 300,
	initComponent : function() {
		var panel = this;
		
		Ext.define('dataModel', {
			extend : 'Ext.data.Model',
			fields : panel.dataModelFields,
			idProperty : 'id'
		});
		var store = new Ext.create('Ext.data.Store', {
			pageSize : panel.pageSize,
			model : 'dataModel',
			remoteSort : true,
			proxy: { 
		        type: 'ajax', 
		        actionMethods: {
	                create : 'POST',
	                read   : 'POST', 
	                update : 'POST',
	                destroy: 'POST'
	            },
		        url : panel.queryUrl,
		        reader: { 
		            type: 'json', 
		            root: 'data', 
		            totalProperty: 'totalCount'
		        }
		     }
		});
		store.load({
			params : {
				start : 0,
				limit : panel.pageSize
			}
		});
		
		this.searchForm = Ext.create('JRainbow.common.SearchForm', {
			searchGrid : panel.gridPanel,
			gridStore : store,
			searchItems : panel.searchItem
		});
		
		var reader = Ext.create('Ext.data.JsonReader', {
	        root : "data", 
	        model : panel.formModel
		});
		
		var createWindow = function(type){
			var defaultConfig = {
				columnWidth : 1 / this.columnNum
			};
			
			var dataForm = Ext.create('Ext.form.Panel', {
				border : false,
				bodyStyle : 'padding:5px',
				layout : 'column',
				defaults : {
					layout : 'form',
					margin: '5 5 5 5',
					columnWidth : 1 / panel.formColumnCount,
					labelWidth : 60,
					labelAlign : 'right'
				},
				reader : reader,
				items : panel.dataItem
			});
			
			var title = '增加信息';
			var bbar = new Ext.Toolbar({
				items : [{ 
					xtype: 'tbfill' 
				},{
					text : '确定',
					iconCls : 'formsave',
					handler : function() {
						dataForm.submit({ 
                            clientValidation:true, 
                            waitMsg:'请稍候', 
                            waitTitle:'正在验证', 
                            url: panel.persistenceUrl, 
                            success:function(form,action){ 
                                Ext.MessageBox.show({ 
                                    width:150, 
                                    title:"成功", 
                                    buttons: Ext.MessageBox.OK, 
                                    msg:action.result.info 
                                }); 
                                window.hide();
                                store.reload();
                            }, 
                            failure:function(form,action){ 
                                Ext.MessageBox.show({ 
                                    width:150, 
                                    title:"失败", 
                                    buttons: Ext.MessageBox.OK, 
                                    msg:action.result.info 
                                }); 
                            } 
                                
                        }); 
					}
				}, {
					text : '取消',
					iconCls : 'cancel',
					handler : function() {
						window.hide();
					}
				},{ 
					xtype: 'tbfill' 
				}]
           });
			
			if(type == 1){
				title = '增加信息';
			}else if(type == 2){
				title = '修改信息';
			}else{
				title = '查看信息';
				bbar = new Ext.Toolbar({
					items : [{ 
						xtype: 'tbfill' 
					},{
						text : '取消',
						iconCls : 'cancel',
						handler : function() {//点击触发的事件
							window.hide();
						}
		            },{ 
						xtype: 'tbfill' 
					}]
		         });
			}
			
			var window = Ext.create('JRainbow.common.FormWindow', {
				title : title,
				border : false,
				form : dataForm,
				width : panel.winWidth,
				height : panel.winHeigth,
				bbar : bbar
			});
			
			return window;
		};
		
		this.addWindow = createWindow(1);
		this.editWindow = createWindow(2);
		this.showWindow = createWindow(3);
		
		var tb = Ext.create('Ext.toolbar.Toolbar');

		tb.add({
			text : '新增',
			iconCls : 'gridadd',
			handler : Ext.Function.bind(panel.addFn, panel)
		});
		tb.add({
			text : '修改',
			iconCls : 'gridupdate',
			handler : Ext.Function.bind(panel.updateFn, panel, [panel.loadUrl])
		});
		tb.add({
			text : '查看',
			iconCls : 'eye',
			handler : Ext.Function.bind(panel.showFn, panel, [panel.loadUrl])
		});
		tb.add({
			text : '删除',
			iconCls : 'griddelete',
			handler : Ext.Function.bind(panel.deleteFn, panel)
		});

		this.gridPanel = Ext.create('Ext.grid.Panel', {
			selType: 'checkboxmodel',
			collapsible: false,
	        animCollapse: false,
			columns : panel.columns,
			plugins: panel.gridPlugins,
			height: document.documentElement.clientHeight - 300,
			tbar : tb,
			bbar : Ext.create('Ext.PagingToolbar', {
				store : store,
				dock:'bottom',
				displayInfo : '当前记录 {0} - {1} of {2}',
				emptyMsg : "暂无记录"
			}),
			region : 'center',
			margins : '5 5 5 5',
			store : store,
			loadMask : true,
			viewConfig : {
				stripeRows : true,
				enableTextSelection : true
			}
		});
		
		Ext.apply(this,{
			border : false,
			layout : 'anchor',
            defaults : {anchor : '100%'},
			items : [{
				xtype : 'panel',
				layout : 'fit',
				items : [this.searchForm]
			},{
				xtype : 'panel',
				layout : 'fit',
				items : [this.gridPanel]
			}]	
		});
				
		this.callParent(arguments);
	},
	addFn : function() {
		this.addWindow.show();
	},
	updateFn : function(loadUrl) {
		if(this.gridPanel.getSelectionModel() != null){
			var record = this.gridPanel.getSelectionModel().getSelection()[0];
			this.editWindow.form.getForm().load({
			      url : loadUrl,
			      params : {
			    	  id : record.get('id')
			      },
			      waitMsg : '加载...'
			});
			this.editWindow.show();
		}
	},
	showFn : function(loadUrl) {
		if(this.gridPanel.getSelectionModel() != null){
			var record = this.gridPanel.getSelectionModel().getSelection()[0];
			this.showWindow.form.getForm().load({
			      url : loadUrl,
			      params : {
			    	  id : record.get('id')
			      },
			      waitMsg : '加载...'
			});
			this.showWindow.show();
		}
		
	},
	deleteFn : function() {
		if(this.gridPanel.getSelectionModel() != null){
			var record = this.gridPanel.getSelectionModel().getSelection()[0];
			var store = this.gridPanel.store;
			Ext.Ajax.request({
                url: this.deleteUrl,
                params: { id: record.get('id') },
                method: 'POST',
                callback: function (options, success, response) {
                    if (success) {
                        store.reload();
                        Ext.Msg.confirm('成功','删除数据成功');
                       
                    } else {
                        Ext.Msg.confirm('失败','删除数据失败');
                    }
                }
            });
		}
	}
});